package com.ssm.service.impl;

import com.ssm.mapper.ProductMapper;
import com.ssm.po.Product;
import com.ssm.po.ProductExample;
import com.ssm.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * ClassName: ProductServiceImpl
 * Package: com.ssm.service
 * Description:
 *
 * @Author 刘英杰
 * @Create 2025/2/10 23:10
 * @Version 1.0
 */

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Override
    @Transactional
    public void updateProduct(Product product) {
        //productMapper.updateByPrimaryKey(product);//如果没有设置该属性,会被置null
        productMapper.updateByPrimaryKeySelective(product);//如果没有设置属性,就保留原来的值
//        int a = 1/0;
    }

    @Override
    public List<Product> findAll() {
        return  productMapper.selectByExample(new ProductExample());//不传任何条件去查询==查询所有
    }

    @Override
    @Transactional
    public void deleteByPid(Integer pid) {
        productMapper.deleteByPrimaryKey(pid);
    }

    @Override
    @Transactional
    public void saveProduct(Product product) {
        productMapper.insert(product);
    }

    @Override
    public Product findById(Integer pid) {
        return productMapper.selectByPrimaryKey(pid);
    }


}
